﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SlideShow.SlideShow
{
    [ToolboxItemAttribute(false)]
    public class SlideShow : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/SlideShow/SlideShow/SlideShowUserControl.ascx";
       
        #region Webppart Properties
        public enum theme
        {
            BlackBckGrdBottomArrow = 0,
            BlackBckGrdSideArrow,
            BlackBckGrdTransBorder,
            WhiteBckGrdNoBorder,
            WhiteBckGrdWhiteBorder
        };
        public enum trans
        {
            elastic = 0,
            fade,
            none

        };
        public enum Animation {
            fadeIn =0,
            fadeInUp,
            fadeInDown,
            fadeInLeft,
            fadeInRight,
            fadeInUpBig,
            fadeInDownBi,
            fadeInLeftBig,
            fadeInRightBig,
            flipInX,
            bounceIn,
            bounceInDown,
            bounceInUp,
            bounceInLeft,
            bounceInRight,
            rotateIn,
            rotateInDownLeft,
            rotateInDownRight,
            rotateInUpLeft,
            rotateInUpRight,
            rollIn,
        };
        protected trans transition;
        protected theme ThemeName;
        protected string listName;
        protected string folderName=string.Empty;
        protected string thumbNail;
        protected string noOfItemsRow;
        protected string noOfItemsCol;
        protected string slideShowSpeed="2500";
        protected string height="768px";
        protected string width="1024px";
        protected string noofitemsperPage="7";
        protected string pausetime="4000";
        protected bool skipJQuery = false;
        protected string siteUrl = "/";
        protected string columnName;
        protected string cacheExpiry="3600";
        protected bool isSlideShow;

        [Personalizable(PersonalizationScope.Shared), Category("SlideShow HomePage Settings"), WebBrowsable, WebDisplayName("SlideShow WebpartTitle:(Html Tags can be used eg:<h3>Title</h3>)")]
        public string WebpartTitle { get; set; }

        [Personalizable(PersonalizationScope.Shared), Category("SlideShow HomePage Settings"), WebBrowsable, WebDisplayName("Items Per Page:")]
        public string NoofItemsperPage
        {
            get { return noofitemsperPage; }

            set
            {
                if (value == null || value == string.Empty)
                    throw new Exception("NUmber of items per page cannot be Empty or Zero!!");
                else
                    
                noofitemsperPage = value;
            }

        }

        [Personalizable(PersonalizationScope.Shared), Category("SlideShow HomePage Settings"), WebBrowsable, WebDisplayName("Animation Effects:")]
        public Animation AnimationEffect
        {
            get;
            set;
        }
        [Personalizable(PersonalizationScope.Shared), Category("SlideShow HomePage Settings"), WebBrowsable, WebDisplayName("Auto rotate page(in milliseconds):")]
        public string AutoRotatePages
        {
            get { return pausetime; }
            set { pausetime = value; }
        }
        [Personalizable(PersonalizationScope.Shared), Category("SlideShow HomePage Settings"), WebBrowsable, WebDisplayName("Thumbnail Size")]
        public string ThumbNailSize
        {
            get { return thumbNail; }
            set { thumbNail = value; }
        }
        [Category("SlideShow HomePage Settings"), WebBrowsable, Personalizable(PersonalizationScope.Shared), WebDisplayName("Skip loading jquery,If already included in Masterpage. "), WebDescription("Skip loading jquery which is included in webpart. Check this property if you are loading jquery on masterpage/page layout")]
        public bool SkipJQuery
        {
            get { return this.skipJQuery; }
            set { this.skipJQuery = value; }

        }

        [Personalizable(PersonalizationScope.Shared),Category("SlideShow"),WebBrowsable, WebDisplayName("Select Theme:")]
        public theme SelectedTheme
        {
            get { return ThemeName; }
            set { ThemeName = value; }
        }
        [Personalizable(PersonalizationScope.Shared),Category("SlideShow"),WebBrowsable, WebDisplayName("Select Transition:")]
        public trans Transition
        {
            get { return transition; }
            set { transition = value; }
        }
        [Personalizable(PersonalizationScope.Shared), Category("SlideShow"), WebBrowsable, WebDisplayName("SlideShow on Clicking the image")]
        public bool IsSlideShow
        {
            get {return isSlideShow ;}
            set { isSlideShow = value; }
        }
        [Personalizable(PersonalizationScope.Shared), Category("SlideShow"), WebBrowsable, WebDisplayName("SlideShow Speed:(in milliseconds)")]
        public string SlideShowSpeed
        {
            get { return slideShowSpeed; }
            set { slideShowSpeed = value; }
        }
        [Personalizable(PersonalizationScope.Shared),Category("SlideShow"),WebBrowsable, WebDisplayName("Image Width")]
        public string ImageWidth
        {
            get { return width; }
            set { width = value; }
        }
        [Personalizable(PersonalizationScope.Shared), Category("SlideShow"), WebBrowsable, WebDisplayName("Image Height")]
        public string ImageHeight
        {
            get { return height; }
            set { height = value; }
        }
        
        [Category("SharePoint List Details"), WebBrowsable, Personalizable(PersonalizationScope.Shared), WebDisplayName("Server relative web sitr url"), WebDescription("Server relative web sitr url, by default it is root site.")]
        public string SiteUrl
        {
            get { return siteUrl; }
            set
            {
                if (value == null || value == string.Empty)
                    throw new Exception("Site url cannot be Empty!!");
                else
                    siteUrl = value;
            }
        }
        [Personalizable(PersonalizationScope.Shared),Category("SharePoint List Details"),WebBrowsable, WebDisplayName("List Name:")]
        public string ListName
        {
            get { return listName; }
            set {
                if (value==null || value==string.Empty)
                    throw new Exception("ListName cannot be Empty!!");
                else
                   listName = value;
            }
        }
        [Personalizable(PersonalizationScope.Shared),Category("SharePoint List Details"),WebBrowsable, WebDisplayName("Images that needs to be fetched from particular folder:")]
        public string FolderName
        {
            get { return folderName; }
            set { folderName = value; }
        }
        [Personalizable(PersonalizationScope.Shared),Category("SharePoint List Details"),WebBrowsable, WebDisplayName("IncludeSubFolder:")]
        public bool IncludeSubFolder { get; set; }
        [Personalizable(PersonalizationScope.Shared), Category("SharePoint List Details"), WebBrowsable, WebDisplayName("Number of Images to fetch:")]
        public string NumberofImages { get; set; }
        [Personalizable(PersonalizationScope.Shared), Category("SharePoint List Details"), WebBrowsable, WebDisplayName("CacheExpiry:(in Seconds)")]
        public string CacheExpiry {
            get {return cacheExpiry ;}
            set { cacheExpiry=value; }
        }
        [Personalizable(PersonalizationScope.Shared), Category("SharePoint List Details"), WebBrowsable, WebDisplayName("Column Name:(Will be displayed below in the slide show)")]
        public string ColumnName
        {
            get { return columnName; }
            set { columnName = value; }
        }
  
        #endregion

        protected override void CreateChildControls()
        {
            
            if (string.IsNullOrEmpty(this.ListName))
            {
                LiteralControl literalCtrl = new LiteralControl(string.Format("<a id='MsoFrameworkToolpartDefmsg_{0}' href=\"javascript:MSOTlPn_ShowToolPane2Wrapper('Edit','129','{0}');\">Edit the Webpart and enter a valid picture library name.", this.ID));
                Controls.Add(literalCtrl);
            }
            else
            {
                Control control = Page.LoadControl(_ascxPath);
                SlideShowUserControl SlideShowUC = (SlideShowUserControl)control;
                SlideShowUC.Listname = ListName;
                SlideShowUC.NumberofImages =Convert.ToInt16(NumberofImages);
                SlideShowUC.FolderName = FolderName;
                SlideShowUC.IncludeFolder = IncludeSubFolder;
                SlideShowUC.ThemeName = SelectedTheme.ToString();
                SlideShowUC.ThumbnailSize = ThumbNailSize;
                SlideShowUC.Transition = Transition.ToString();
                SlideShowUC.Height = ImageHeight;
                SlideShowUC.Width = ImageWidth;
                SlideShowUC.SlideShowSpeed = SlideShowSpeed;
                SlideShowUC.CacheExpiry = CacheExpiry;
                SlideShowUC.SlideShow = IsSlideShow;
                SlideShowUC.ColumnName = ColumnName;
                SlideShowUC.SlideShowTitle = WebpartTitle;
                SlideShowUC.Animation = AnimationEffect.ToString();
                SlideShowUC.ItemsperPage = NoofItemsperPage;
                SlideShowUC.Pause = AutoRotatePages;
                SlideShowUC.skipJQuery = SkipJQuery;
                SlideShowUC.Siteurl = SiteUrl;
                Controls.Add(control);
            }
        }

        
    }
}
